import SwiftUI
import UIKit // 导入UIKit便于使用TCP相关功能

// 设置视图
struct SettingsView: View {
    @EnvironmentObject var connectionSettings: ConnectionSettings
    @EnvironmentObject var tcpCarService: TCPCarService
    @Binding var showDisconnectAlert: Bool
    
    var body: some View {
        Form {
            // 连接信息部分
            Section(header: Text("TCP连接信息")) {
                HStack {
                    Text("IP地址")
                    Spacer()
                    Text(connectionSettings.ipAddress)
                        .foregroundColor(.secondary)
                }
                
                HStack {
                    Text("控制端口")
                    Spacer()
                    Text(connectionSettings.port)
                        .foregroundColor(.secondary)
                }
                
                HStack {
                    Text("摄像头端口")
                    Spacer()
                    Text(connectionSettings.cameraPort)
                        .foregroundColor(.secondary)
                }
                
                HStack {
                    Text("连接状态")
                    Spacer()
                    Text(tcpCarService.isConnected ? "已连接" : "未连接")
                        .foregroundColor(tcpCarService.isConnected ? .green : .red)
                }
            }
            
            // 断开连接按钮
            Section {
                Button(action: {
                    showDisconnectAlert = true
                }) {
                    HStack {
                        Spacer()
                        Text("断开与小车的连接")
                            .foregroundColor(.red)
                        Spacer()
                    }
                }
            }
            
            // 应用信息部分
            Section(header: Text("应用信息")) {
                HStack {
                    Text("版本")
                    Spacer()
                    Text("1.0.0")
                        .foregroundColor(.secondary)
                }
            }
        }
    }
}

// 预览无法直接使用，因为有@Binding属性
struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView(showDisconnectAlert: .constant(false))
            .environmentObject(ConnectionSettings())
            .environmentObject(TCPCarService())
    }
} 